<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib tagdir="/WEB-INF/tags/sys"  prefix="sys"%>
<%@ taglib uri="/WEB-INF/tlds/webui.tld"  prefix="webui"%>

<%--采纳状态 --%>
<webui:enum var="isAdoptedType" code="isAdoptedType" appendJs="true"/>
<%--是否独家 --%>
<webui:enum var="exclusiveType" code="exclusive" appendJs="true"/>
<%--内容分类 --%>
<webui:enum var="contentType" code="contentType" appendJs="true"/>
<%--内容状态 --%>
<webui:enum var="statusType" code="statusType" appendJs="true"/>

<%--电影地区 --%>
<webui:enum var="vodArea" code="vodArea" appendJs="true"/>
<%--电视剧地区 --%>
<webui:enum var="seriesArea" code="seriesArea" appendJs="true"/>

<%--电视剧类型 --%>
v
<webui:enum var="vodType" code="vodType" appendJs="true"/>
<script>
function dataLoad(table,data){
	var contentCount = data.page.totalElements;
	if(!document.getElementById("table_length")){
		$("#grid_cpContent_paginate").parent().prev().append("<div id='table_length' style='float:left'></div>");
	}
	if (contentCount > 0) {
		$("#table_length").html("共"+contentCount+"条数据");
	} else {
		$("#table_length").html("");
	}
}
</script>
<webui:panel id="cpContentSearch">
	<webui:form styleClass="form-horizontal" id="cpContentSearchForm"
		action="${contextPath}/declare/content/findContent.action"
		name="cpContentSearchForm" method="post">
		<input type="hidden" name="orderBy" id="orderBy"/>
		<input type="hidden" name="field" id="field"/>
		<webui:input label="节目名" styleClass="col-sm-4" inputClass="col-sm-9"
			labelClass="col-sm-3" type="text" name="name">
		</webui:input>

		<webui:select label="内容分类" name="contentType" id=""
			styleClass="col-sm-3" labelClass="col-sm-4" inputClass="col-sm-8"
			lookup="${contentType}">
		</webui:select>
		<webui:datepicker label="申报时间" styleClass="col-sm-4"
			labelClass="col-sm-3" inputClass="col-sm-9"
			beginName="declareTimeBegin" beginId="" endName="declareTimeEnd"
			endId="" duration="true">
		</webui:datepicker>
		<webui:button label="" type="button" styleClass="col-sm-1"
			labelClass="col-sm-7" inputClass="col-sm-5"
			buttonClass="btn-primary" onclick="grid_cpContent.filter()">查询
		</webui:button>
		<div class="col-sm-12"></div>
	</webui:form>
	<webui:table id="grid_cpContent"
		dataUrl="${contextPath}/declare/content/findContent.action?adoptSign=order"
		datas="data" editorForm="#editCpContent" dataload="dataLoad"
		searchForm="#cpContentSearch">
		<webui:column field="name" title="片名">{{row.name}}</webui:column>
		<webui:column field="declareTime" title="申报时间">{{row.declareTime | moment('YYYY-MM-DD HH:mm:ss')}}</webui:column>
		<webui:column field="code" title="编号">{{row.code}}</webui:column>
		<webui:column field="externalCode" title="外部编码">{{row.externalCode}}</webui:column>
		<webui:column field="startTime" title="跳过开头">{{row.startTime}}</webui:column>
		<webui:column field="endTime" title="跳过结尾">{{row.endTime}}</webui:column>
		<webui:column field="contentType" title="分类" lookup="${contentType }">{{row.contentTypeDesc}}</webui:column>
		<webui:column field="exclusive" title="是否独家" lookup="${exclusiveType }">{{row.exclusiveDesc}}</webui:column>
		<webui:column field="status" title="状态"  lookup="${statusType }" >{{row.statusDesc}}</webui:column>
		<webui:column field="isAdopted" title="采纳状态" lookup="${isAdoptedType }">{{row.isAdoptedDesc}}</webui:column>
		<webui:column title="填充情况">
			<span v-if="row.externalCode!=''&&row.startTime!=''&&row.endTime!=''&&row.code!=''">已填充</span>
			<span v-else>未填充</span>
		</webui:column>
		<webui:column field="" title="操作">
			<a class="btn btn-xs btn-primary" v-bind:href="'javascript:editContent('+row.id+')'">填充</a>
		</webui:column>
	</webui:table>
</webui:panel>
<webui:panel id="editCpContent" style="display:none;" title="填充内容">
	<webui:form method="post" styleClass="form-horizontal" id="editCpContentForm">
		<input type="hidden" id="editFromContentId" name="id" /><br>
		<webui:input label="编号" id="editFromCode"
			name="code" placeholder="请输入格式为：Program_数字   的编号"
			type="text" inputClass="col-sm-4" labelClass="col-sm-4">
			<webui:validator required="true"></webui:validator>
		</webui:input>
		<webui:input label="外部编码" id="editFromExternalCode"
			name="externalCode" placeholder="请输入32位的数字编码"
			type="text" inputClass="col-sm-4" labelClass="col-sm-4">
			<webui:validator digits="true" maxlength="32" minlength="32" required="true"></webui:validator>
		</webui:input>
		<div class="col-sm-12"></div>
		<webui:input label="跳过开头" id="editFromStartTime" name="startTime"
			placeholder="00:00:00" type="text"
			inputClass="col-sm-4" labelClass="col-sm-4">
			<webui:validator required="true"></webui:validator>
		</webui:input>
		<div class="col-sm-12"></div>
		<webui:input label="跳过结尾" id="editFromEndTime" name="endTime"
			placeholder="00:00:00" type="text"
			inputClass="col-sm-4" labelClass="col-sm-4">
			<webui:validator required="true"></webui:validator>
		</webui:input>
	</webui:form>
	<div class="row">
		<div class="col-md-7" style="float: right;">
			<webui:button type="button" buttonClass="btn-white"
				onclick="cancelCpContent()">取消</webui:button>
			<webui:button id="contentSaveBtn" type="button"
				buttonClass="btn-primary" ladda="true" onclick="saveContent()">保存</webui:button>
		</div>
	</div>
</webui:panel>
<script>
window.onload=function(){
	$("#editFromExternalCode").keyup(function(){
		var externalCode = $("#editFromExternalCode").val();
		if(externalCode.length > 32){
			$("#editFromExternalCode").val(externalCode.substring(0,32));
		}
	});
}
function editContent(id) {
	$.ajax({
		type : "GET",
		async : false,
		url : "${contextPath}/rest/cPContents/"+id,
		success : function(data){
			grid_cpContent.create();
			$("#editFromContentId").val(data.id);
			$("#editFromCode").val(data.code);
			$("#editFromExternalCode").val(data.externalCode);
			$("#editFromStartTime").val(data.startTime);
			$("#editFromEndTime").val(data.endTime);
		}
	});
}
function cancelCpContent() {
	grid_cpContent.show();
	$("#editCpContentForm").reset();
	editCpContentFormValidator.resetForm();
}
function saveContent() {
	if (returnCheck() && editCpContentFormValidator.form()) {
		contentSaveBtn.ladda('start');
		$.ajax({
			method : "post",
			url : '${contextPath}/declare/cpContent/fill.action',
			dataType : "json",
			contentType : "application/json; charset=utf-8",
			data : $('form#editCpContentForm').serializeJSON(),
			success : function(data) {
				contentSaveBtn.ladda('stop');
				if(data.result==0){
					showMessage('提示','保存成功。', function(){
						$('#editCpContentForm').reset();
						grid_cpContent.refresh();
					});
				}else{
					showError('提示','保存失败'+"，错误信息："+data.message, function() {
						$('#editCpContentForm').reset();
						grid_cpContent.refresh();
					});
				}
			}
		});
	}
}
function onCheckCode(id) {
	var reg = /^[P][r][o][g][r][a][m][_]\d+$/;
	var code = $(id).val();
	if(code != ''){
		if (reg.test(code)) {
			var check = true;
			var conId = $("#editFromContentId").val() == "" ? "" : $("#editFromContentId").val();
			var url = '${contextPath}/declare/cpContent/findByCode.action?code='+code+'&conId='+conId
			$.ajax({
				url:url,
				type:'get',
				async : false,
				success : function(data){
					if(data){
						outCheckError("error", "该编码已经存在", id);
						check = false;
					}else{
						outCheckError("correct", "", id);
						check = true;
					}
				}
			});
			return check;
		} else {
			outCheckError("error", "请输入格式为：Program_+数字  的编号", id);
			return false;
		}
	}else{
		return true;
	}
}
//验证externalCode
function onCheckExternalCode(id) {
	var externalCode = $(id).val();
	var pass = true;
	if(externalCode != ''){
		var conId = $("#editFromContentId").val() == "" ? null : $("#editFromContentId").val();
		var url = '${contextPath}/declare/cpContent/findByExternalCode.action?externalCode='+externalCode+'&conId='+conId;
		$.ajax({
			method:"get",
			url:url,
			async:false,
			success:function(data){
				if(data){
					outCheckError("error", "该外部编码已经存在", id);
					pass = false;
				}else{
					outCheckError("correct", "", id);
					pass = true;
				}
			}
		});
		return pass;
	}else{
		return true;
	}
}
//跳过开头和结尾的时间格式验证
function onCheckTime(id) {
	var reg = /^([0-9][0-9]):([0-5][0-9]):([0-5][0-9])$/;
	var time = $(id).val();
	if (time != "") {
		if (reg.test(time)) {
			outCheckError("correct", "", id);
			return true;
		} else {
			outCheckError("error", "请输入规范的时间格式如00:00:00", id);
			return false;
		}
	} else {
		outCheckError("correct", "", id);
		return true;
	}
}
//错误提示
function outCheckError(state, message, id) {
	if (state == "error") {
		$(id).addClass("error");
		$(id).next().html(message).css("display", "block");
	} else if (state == "correct") {
		$(id).removeClass("error");
		$(id).next().html(message).css("display", "none");;
	}
}
//提交校验
function returnCheck() {
	var pass = true;
	if(!onCheckCode("#editFromCode")){
		pass = false;
	}
	if(!onCheckExternalCode("#editFromExternalCode")){
		pass = false;
	}
	if(!onCheckTime("#editFromStartTime")){
		pass = false;
	}
	if(!onCheckTime("#editFromEndTime")){
		pass = false;
	}
	return pass;
}
</script>